package com.moxiao.yfjh.module.order.controller.admin.blindTradeOrder;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeOrder.vo.*;
import com.moxiao.yfjh.module.order.convert.blindTradeOrder.BlindTradeOrderConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.blindTradeOrder.BlindTradeOrderDO;
import com.moxiao.yfjh.module.order.tradeRefund.blindTradeOrder.BlindTradeOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲交易订单")
@RestController
@RequestMapping("/order/blind-trade-order")
@Validated
public class BlindTradeOrderController {

    @Resource
    private BlindTradeOrderService blindTradeOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲交易订单")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:create')")
    public CommonResult<Long> createBlindTradeOrder(@Valid @RequestBody BlindTradeOrderCreateReqVO createReqVO) {
        return success(blindTradeOrderService.createBlindTradeOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲交易订单")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:update')")
    public CommonResult<Boolean> updateBlindTradeOrder(@Valid @RequestBody BlindTradeOrderUpdateReqVO updateReqVO) {
        blindTradeOrderService.updateBlindTradeOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲交易订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:delete')")
    public CommonResult<Boolean> deleteBlindTradeOrder(@RequestParam("id") Long id) {
        blindTradeOrderService.deleteBlindTradeOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲交易订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:query')")
    public CommonResult<BlindTradeOrderRespVO> getBlindTradeOrder(@RequestParam("id") Long id) {
        BlindTradeOrderDO blindTradeOrder = blindTradeOrderService.getBlindTradeOrder(id);
        return success(BlindTradeOrderConvert.INSTANCE.convert(blindTradeOrder));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲交易订单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:query')")
    public CommonResult<List<BlindTradeOrderRespVO>> getBlindTradeOrderList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindTradeOrderDO> list = blindTradeOrderService.getBlindTradeOrderList(ids);
        return success(BlindTradeOrderConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲交易订单分页")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:query')")
    public CommonResult<PageResult<BlindTradeOrderRespVO>> getBlindTradeOrderPage(@Valid BlindTradeOrderPageReqVO pageVO) {
        PageResult<BlindTradeOrderDO> pageResult = blindTradeOrderService.getBlindTradeOrderPage(pageVO);
        return success(BlindTradeOrderConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲交易订单 Excel")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-order:export')")
    @OperateLog(type = EXPORT)
    public void exportBlindTradeOrderExcel(@Valid BlindTradeOrderExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlindTradeOrderDO> list = blindTradeOrderService.getBlindTradeOrderList(exportReqVO);
        // 导出 Excel
        List<BlindTradeOrderExcelVO> datas = BlindTradeOrderConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲交易订单.xls", "数据", BlindTradeOrderExcelVO.class, datas);
    }

}
